import chen.User2;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        String username = request.getParameter("Username");
        String name =  request.getParameter("Name");
        String email = request.getParameter("Email");
        String password = request.getParameter("Password");

        Jsonb jsonb = JsonbBuilder.create();
        User2 UserDTO = new User2(username,email,password," ","boy.jpg",name);
        String str = jsonb.toJson(UserDTO);

        System.out.println(str);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/main/User.txt", true)));
            out.write(str+"\n");
            out.close();
        } catch (Exception e) {
            response.sendRedirect("/error.html");
        }

        response.sendRedirect("/login.html");
    }
}
